package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.ProductInfo_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class ProductInfoController extends Controller {

    private final FormFactory formFactory;
    private final ProductInfo_Repository tProductInfoRepository;
    private final HttpExecutionContext ec;

    @Inject
    public ProductInfoController(FormFactory formFactory, ProductInfo_Repository tProductInfoRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tProductInfoRepository = tProductInfoRepository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> getProductByID() {
		JsonNode json = request().body().asJson();
		Integer ProductID = json.findPath("ProductID").intValue();
	    return tProductInfoRepository.getProductByID(ProductID).thenApplyAsync(_tProductInfoEntity -> {
	    	return ok(Json.toJson(_tProductInfoEntity));
	    }, ec.current());
    }
    
    public CompletionStage<Result> getProductList() {
	    return tProductInfoRepository.getProductList().thenApplyAsync(_tProductInfoEntity -> {
	    	return ok(Json.toJson(_tProductInfoEntity));
	    }, ec.current());
    }

    public CompletionStage<Result> getItemByType() {
        JsonNode json = request().body().asJson();
        String ItemType = json.findPath("ItemType").textValue();

        return tProductInfoRepository.getItemByType(ItemType).thenApplyAsync(result -> {
            return ok(Json.toJson(result));
        }, ec.current());
    }
}
